#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "function.h"

#pragma execution_character_set("utf-8")

#include <QMessageBox>
#include <QFileDialog>
#include <QCloseEvent>
#include <QDebug>
#include <QTime>
#include <QDir>
#include <QFile>


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    globalMark=0;
}

MainWindow::~MainWindow()
{
    delete ui;
}

//打开图片
void MainWindow::on_action_open_triggered()
{
    //判断展示图片的框里是否有图片，如果有图片判断是否保存，若图片未保存不能打开新图片
    if(globalMark == 1) {
        if(proImage.getLastImage() != proImage.getFinalImage()) {
            QMessageBox mes;
            mes.warning(this,"提示信息","当前图片未保存，请先保存再打开新的图片!");
            return;
        }
        else globalMark = 0;
    }
    //清除展示图片的框
    ui->show_label->clear();
     //打开图片
    QString fileName = QFileDialog::getOpenFileName(
                this, tr("open image file"),
                "./", tr("Image files(*.bmp);;All files (*.*)"));

    if(fileName.isEmpty())
    {
        QMessageBox mes;
        mes.warning(this,"提示信息","打开图片失败!");
        return;
    }
    else
    {
        QImage image(fileName);
//        image=image.scaled(ui->show_label->width(),ui->show_label->height(),Qt::KeepAspectRatio);//Qt::KeepAspectRatio自适应大小，不变形
        ui->show_label->setPixmap(QPixmap::fromImage(image));
        if(image.isNull())
        {
            QMessageBox::information(this,"提示信息","打开文件失败");
        }
        else
        {
            globalMark=1;//存储标记，用于判断是否有文件
            globalImage=image;
            proImage.copyPicture(image);
        }
    }

}

//保存图片,保存至后无法撤销
void MainWindow::on_action_save_triggered()
{
    if( globalMark==1)////如果标记为1，证明有图片正在被编辑，不然没有则不需要保存
    {
        QString filePath;//存储保存路径
        QString saveName="保存实验图";//保存图片名字
        QDateTime time = QDateTime::currentDateTime();//获取系统现在的时间
        QString str = time.toString("yyyyMMdd_hhmmss"); //设置显示格式
        saveName += str;
        filePath = QFileDialog::getExistingDirectory(this,"");//获取文件夹路径
        if(filePath.isEmpty())
        {
            QMessageBox::information(this,"提示信息","保存失败");
        }
        else
        {
            QImage images;
            images=globalImage;//获取修改的图片

            //将图片重新命名并保存至文件夹里
            QString savePath=QString("%1//%2.bmp").arg(filePath).arg(saveName);
            images.save(savePath);//QImage保存方法
            QMessageBox::information(this,"提示信息","保存成功");
            proImage.setLastImage(proImage.getFinalImage());
        }
    }
    else
    {
        QMessageBox::information(this,"提示信息","保存失败");
    }
}


void MainWindow::on_action_back_triggered()
{
    if(proImage.getLastImage() == proImage.getFinalImage())
        return;
    proImage.setFinalImage(proImage.getLastImage());
    ui->show_label->clear();//先清除展示图片的框
    ui->show_label->setPixmap(QPixmap::fromImage(proImage.getFinalImage()));
}

void MainWindow::on_action_close_triggered()
{
    //判断展示图片的框里是否有图片，如果有图片判断是否保存，若图片未保存不能关闭
    if(globalMark == 1) {
        if(proImage.getLastImage() != proImage.getFinalImage()) {
            QMessageBox::StandardButton result=QMessageBox::question(this, "提示信息", "当前图片未保存，确定要继续退出吗？",
                                  QMessageBox::Yes|QMessageBox::No |QMessageBox::Cancel);
                if (result==QMessageBox::Yes)
                    qApp->quit();
                else
                    return;
        }
    }
    QMessageBox::StandardButton result=QMessageBox::question(this, "提示信息", "确定要退出吗？",
                          QMessageBox::Yes|QMessageBox::No |QMessageBox::Cancel);
    if (result==QMessageBox::Yes)
        qApp->quit();
    else
        return;
}

void MainWindow::on_action_expand_triggered()
{
    QImage image = proImage.expandPicture(380);
    ui->show_label->clear();
    ui->show_label->setPixmap(QPixmap::fromImage(image));
}

void MainWindow::on_action_shrink_triggered()
{
    QImage image = proImage.expandPicture(50);
    ui->show_label->clear();
    ui->show_label->setPixmap(QPixmap::fromImage(image));
}

void MainWindow::on_action_revolve_triggered()
{
    QImage image = proImage.revolvePicture(120);
    ui->show_label->clear();
    //image=image.scaled(ui->show_label->width(),ui->show_label->height(),Qt::KeepAspectRatio);//Qt::KeepAspectRatio自适应大小，不变形
    ui->show_label->setPixmap(QPixmap::fromImage(image));
}



